package com.wangce.controller;

import com.github.pagehelper.PageInfo;
import com.wangce.entity.Dim;
import com.wangce.entity.Menu;
import com.wangce.entity.Order;
import com.wangce.entity.User;
import com.wangce.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * ClassName: OrderController
 * Description:
 * date: 2021/11/29 9:38
 *
 * @author 凡人
 * @since JDK 1.8
 */
@Controller
public class OrderController {

    @Autowired
    OrderService service;

    /**
     * 主页面
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "index";
    }

    /**
     * 列表
     * @param model
     * @param d
     * @param cur
     * @param size
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Dim d, @RequestParam(defaultValue = "1")Integer cur,
                       @RequestParam(defaultValue = "3")Integer size){
        PageInfo<Order> list = service.list(d,cur,size);
        model.addAttribute("list",list);
        model.addAttribute("d",d);
        return "list";
    }

    /**
     * 菜单
     * @return
     */
    @RequestMapping("getTree")
    @ResponseBody
    public List<Menu> getTree(){
        List<Menu> list = service.getTree();
        return list;
    }

    /**
     * 登陆
     * @return
     */
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 登陆
     * @return
     */
    @RequestMapping("login")
    public String login(HttpSession session, User u){
        User user = service.login(u);
        if(user != null){
            session.setAttribute("user",user);
            return "redirect:index";
        }
        return "login";
    }

    /**
     * 添加
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }

    /**
     * 添加
     * @param o
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public String add(HttpSession session, Order o){
        User user = (User) session.getAttribute("user");
        if(user !=null){
            o.setUid(user.getId());
        }
        Integer i = service.add(o);
        if(i>0){
            return "ok";
        }
        return "no";
    }

    /**
     * 图片
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("upFile")
    @ResponseBody
    public String upFile(MultipartFile file) throws IOException {
        if(file!=null && !file.isEmpty()){
            String name = file.getOriginalFilename();
            String suffx = name.substring(name.lastIndexOf("."));
            String newName = UUID.randomUUID() + suffx;
            file.transferTo(new File("d:/pic",newName));
            return "/pic/"+newName;
        }
        return null;
    }
}
